package com.inibb.app.po.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.inibb.app.po.DomainDefinition;
import com.inibb.app.po.IdEntity;
import com.inibb.app.util.CollectionUtils;






@Entity
@Table(name = "USERS")
public class User extends IdEntity {

	private String loginName;

	private String password;

	private String name;

	private String email;

	private Collection<Role> roles = new LinkedHashSet<Role>();
	
	//private Long groupId;
	
	private String activeFlag;
	
	private Date regDate;
	
	private Date effectiveFrom;
	
	private Date effectiveTo;
	
	
	/*@Column( name="GROUP_ID")
	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}*/

	@Column( name="LOGIN_NAME", length=20 )
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@Column( name="PASSWORD",length=64 )
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column( name="NAME", length=20 )
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column( name="EMAIL", length=50 )
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinTable(
	   name = "USERS_ROLES", 
	   joinColumns = { @JoinColumn(name = "USER_ID") }, 
	   inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") }
	)
	@OrderBy( "id" )
	public Collection<Role> getRoles() {
		return roles;
	}

	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}

	@Transient
	public String getRoleNames() throws Exception {
		return CollectionUtils.fetchPropertyToString(roles, "name", ", ");
	}

	@SuppressWarnings("unchecked")
	@Transient
	public List<Long> getRoleIds() throws Exception {
		return CollectionUtils.fetchPropertyToList(roles, "id");
	}
	@Transient
	public String[] getRoleIdsAsArray() throws Exception {
		List<String> arrStr = new ArrayList<String>(); 
		for(Long id:getRoleIds()){
			arrStr.add(String.valueOf(id));
		}
		return arrStr.toArray(new String[0]);
	}
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
    @NotNull
	@Column(name = "ACTV_FLG", length=DomainDefinition.STATUS_LENGTH, unique = false, nullable = false)
	public String getActiveFlag() {
		return activeFlag;
	}
	public void setActiveFlag(String activeFlag) {
		this.activeFlag = activeFlag;
	}

	@Column(name = "REG_DATE")
	public Date getRegDate() {
		return regDate;
	}

	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}

	@Column(name = "EFF_FROM")
	public Date getEffectiveFrom() {
		return effectiveFrom;
	}

	public void setEffectiveFrom(Date effectiveFrom) {
		this.effectiveFrom = effectiveFrom;
	}
	@Column(name = "EFF_TO")
	public Date getEffectiveTo() {
		return effectiveTo;
	}

	public void setEffectiveTo(Date effectiveTo) {
		this.effectiveTo = effectiveTo;
	}
	
	
	
}